var curr_id = 0;
var load = 1;
var buttons;
var t_time;
var rank;
$("document").ready(function() {
	load_tweets();
	setInterval(load_tweets, 20000);
});
function reply_vote(vote,iid){
        $.post("post.php", { vote : vote, iid : iid});
        //$.post("http://cloudyama.com/sites/circles/cloud/post.php")
}
//function show_textbox(){
//	$("#resultmessage").html("<div style='display:block; background-color: Green; color: White; font-size: 15px; font-weight: bold; padding: 0px 0px 0pxh0px;'>Saved!")
//	$("#resultmessage").show('slow');
//}
function load_tweets() {
	var result = '';	
	$.get('ranking.php', function(data){
			$(".tweet_result").remove();		
			$(".buttons").remove();	
			$(".result_box").remove();	
			$("div#rank").remove();	
			$("topic",data).each(function(){
				curr_id = parseInt($(this).find("id").text())	
				buttons = "<div class = 'buttons'><div id = 'like'><a href='javascript:reply_vote(1," + curr_id +");'>Like</a></div><div id = 'unlike'><a href='javascript:reply_vote(-1,"+ curr_id +");'>Unlike</a></div><div id = 'reply'><a href='javascript:show_textbox();'>Discuss in Forum</a></div>";	
				var title = $(this).find("title").text();
				rank = "<div id = 'rank'>" + $(this).find("rank").text() + "</div>";
				if ( title !='' ){
						result = result + "<div class = result_box>"+ rank + "<div class='tweet_result'>" + linkify(title) + "</div>" + buttons+ "</div></div>";
				}  //$("#twitter_results").append(result);	
			});
			$("#ranking").append(result);	
		//$("#twitter_results").prepend(result);	
	});
}

function linkify(text) {
    // modified from TwitterGitter by David Walsh (davidwalsh.name)
    // courtesy of Jeremy Parrish (rrish.org)
    return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
               .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
               .replace(/(^|\W)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
}
